﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using IPC.Communication.Framework.BasicFramework;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.IMessage;
using IPC.Communication.Framework.Core.Net;
using IPC.Communication.Framework.Profinet.AllenBradley;
using IPC.Communication.Framework.Reflection;

namespace IPC.Communication.Framework.Profinet.Omron
{
    public class OmronConnectedCipNet : NetworkDeviceBase
    {
        private uint OTConnectionId = 0u;

        private SoftIncrementCount incrementCount = new SoftIncrementCount(65535L, 0L);

        private Random random = new Random();

        public uint SessionHandle
        {
            get;
            protected set;
        }

        public string ProductName
        {
            get;
            private set;
        }

        public OmronConnectedCipNet()
        {
            base.WordLength = 2;
            base.ByteTransform = new RegularByteTransform();
        }

        public OmronConnectedCipNet(string ipAddress, int port = 44818)
        {
            IpAddress = ipAddress;
            Port = port;
            base.WordLength = 2;
            base.ByteTransform = new RegularByteTransform();
        }

        protected override INetMessage GetNewNetMessage()
        {
            return new AllenBradleyMessage();
        }

        protected override byte[] PackCommandWithHeader(byte[] command)
        {
            return AllenBradleyHelper.PackRequestHeader(112, SessionHandle, AllenBradleyHelper.PackCommandSpecificData(GetOTConnectionIdService(), command));
        }

        protected override OperateResult InitializationOnConnect(Socket socket)
        {
            OperateResult<byte[]> operateResult = ReadFromCoreServer(socket, AllenBradleyHelper.RegisterSessionHandle(), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult operateResult2 = AllenBradleyHelper.CheckResponse(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return operateResult2;
            }
            SessionHandle = base.ByteTransform.TransUInt32(operateResult.Content, 4);
            OperateResult<byte[]> operateResult3 = ReadFromCoreServer(socket, AllenBradleyHelper.PackRequestHeader(111, SessionHandle, GetLargeForwardOpen()), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult3.IsSuccess)
            {
                return operateResult3;
            }
            if (operateResult3.Content[42] != 0)
            {
                if (base.ByteTransform.TransUInt16(operateResult3.Content, 44) == 256)
                {
                    return new OperateResult("Connection in use or duplicate Forward Open");
                }
                return new OperateResult("Forward Open failed, Code: " + base.ByteTransform.TransUInt16(operateResult3.Content, 44).ToString());
            }
            OTConnectionId = base.ByteTransform.TransUInt32(operateResult3.Content, 44);
            incrementCount.ResetCurrentValue();
            OperateResult<byte[]> operateResult4 = ReadFromCoreServer(socket, AllenBradleyHelper.PackRequestHeader(111, SessionHandle, GetAttributeAll()), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult4.IsSuccess)
            {
                return operateResult4;
            }
            if (operateResult4.Content.Length > 59)
            {
                ProductName = Encoding.UTF8.GetString(operateResult4.Content, 59, operateResult4.Content[58]);
            }
            return OperateResult.CreateSuccessResult();
        }

        protected override OperateResult ExtraOnDisconnect(Socket socket)
        {
            OperateResult<byte[]> operateResult = ReadFromCoreServer(socket, AllenBradleyHelper.UnRegisterSessionHandle(SessionHandle), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            return OperateResult.CreateSuccessResult();
        }

        //[AsyncStateMachine(typeof(<InitializationOnConnectAsync>d__6))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
        //{
        //    <InitializationOnConnectAsync>d__6 stateMachine = new <InitializationOnConnectAsync>d__6();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ExtraOnDisconnectAsync>d__7))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> ExtraOnDisconnectAsync(Socket socket)
        //{
        //    <ExtraOnDisconnectAsync>d__7 stateMachine = new <ExtraOnDisconnectAsync>d__7();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        private byte[] GetOTConnectionIdService()
        {
            byte[] array = new byte[8]
		{
			161,
			0,
			4,
			0,
			0,
			0,
			0,
			0
		};
            base.ByteTransform.TransByte(OTConnectionId).CopyTo(array, 4);
            return array;
        }

        private OperateResult<byte[]> BuildReadCommand(string[] address, ushort[] length)
        {
            try
            {
                List<byte[]> list = new List<byte[]>();
                for (int i = 0; i < address.Length; i++)
                {
                    list.Add(AllenBradleyHelper.PackRequsetRead(address[i], length[i], isConnectedAddress: true));
                }
                return OperateResult.CreateSuccessResult(PackCommandService(list.ToArray()));
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        private OperateResult<byte[]> BuildWriteCommand(string address, ushort typeCode, byte[] data, int length = 1)
        {
            try
            {
                return OperateResult.CreateSuccessResult(PackCommandService(AllenBradleyHelper.PackRequestWrite(address, typeCode, data, length, isConnectedAddress: true)));
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        private byte[] PackCommandService(params byte[][] cip)
        {
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.WriteByte(177);
            memoryStream.WriteByte(0);
            memoryStream.WriteByte(0);
            memoryStream.WriteByte(0);
            long currentValue = incrementCount.GetCurrentValue();
            memoryStream.WriteByte(BitConverter.GetBytes(currentValue)[0]);
            memoryStream.WriteByte(BitConverter.GetBytes(currentValue)[1]);
            if (cip.Length == 1)
            {
                memoryStream.Write(cip[0], 0, cip[0].Length);
            }
            else
            {
                memoryStream.Write(new byte[6]
			{
				10,
				2,
				32,
				2,
				36,
				1
			}, 0, 6);
                memoryStream.WriteByte(BitConverter.GetBytes(cip.Length)[0]);
                memoryStream.WriteByte(BitConverter.GetBytes(cip.Length)[1]);
                int num = 2 + cip.Length * 2;
                for (int i = 0; i < cip.Length; i++)
                {
                    memoryStream.WriteByte(BitConverter.GetBytes(num)[0]);
                    memoryStream.WriteByte(BitConverter.GetBytes(num)[1]);
                    num += cip[i].Length;
                }
                for (int j = 0; j < cip.Length; j++)
                {
                    memoryStream.Write(cip[j], 0, cip[j].Length);
                }
            }
            byte[] array = memoryStream.ToArray();
            memoryStream.Dispose();
            BitConverter.GetBytes((ushort)(array.Length - 4)).CopyTo(array, 2);
            return array;
        }

        private OperateResult<byte[], ushort, bool> ReadWithType(string[] address, ushort[] length)
        {
            OperateResult<byte[]> operateResult = BuildReadCommand(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult);
            }
            OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult2);
            }
            OperateResult operateResult3 = AllenBradleyHelper.CheckResponse(operateResult2.Content);
            if (!operateResult3.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult3);
            }
            return ExtractActualData(operateResult2.Content, isRead: true);
        }

        public OperateResult<byte[]> ReadCipFromServer(params byte[][] cips)
        {
            byte[] send = PackCommandService(cips.ToArray());
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult operateResult2 = AllenBradleyHelper.CheckResponse(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content);
        }

        public OperateResult<T> ReadStruct<T>(string address) where T : struct
        {
            OperateResult<byte[]> operateResult = Read(address, 1);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<T>(operateResult);
            }
            return HslHelper.ByteArrayToStruct<T>(operateResult.Content.RemoveBegin(2));
        }

        //[AsyncStateMachine(typeof(<ReadWithTypeAsync>d__24))]
        //[DebuggerStepThrough]
        //private Task<OperateResult<byte[], ushort, bool>> ReadWithTypeAsync(string[] address, ushort[] length)
        //{
        //    <ReadWithTypeAsync>d__24 stateMachine = new <ReadWithTypeAsync>d__24();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[], ushort, bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCipFromServerAsync>d__25))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadCipFromServerAsync(params byte[][] cips)
        //{
        //    <ReadCipFromServerAsync>d__25 stateMachine = new <ReadCipFromServerAsync>d__25();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.cips = cips;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStructAsync>d__26<>))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<T>> ReadStructAsync<T>(string address) where T : struct
        //{
        //    <ReadStructAsync>d__26<T> stateMachine = new <ReadStructAsync>d__26<T>();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            OperateResult<byte[], ushort, bool> operateResult = ReadWithType(new string[1]
		{
			address
		}, new ushort[1]
		{
			length
		});
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content1);
        }

        [HslMqttApi("ReadMultiAddress", "")]
        public OperateResult<byte[]> Read(string[] address, ushort[] length)
        {
            OperateResult<byte[], ushort, bool> operateResult = ReadWithType(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content1);
        }

        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            if (length == 1 && !Regex.IsMatch(address, "\\[[0-9]+\\]$"))
            {
                OperateResult<byte[]> operateResult = Read(address, length);
                if (!operateResult.IsSuccess)
                {
                    return OperateResult.CreateFailedResult<bool[]>(operateResult);
                }
                return OperateResult.CreateSuccessResult(SoftBasic.ByteToBoolArray(operateResult.Content));
            }
            OperateResult<byte[]> operateResult2 = Read(address, length);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<bool[]>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(operateResult2.Content.Select((byte m) => m != 0).Take(length).ToArray());
        }

        [HslMqttApi("ReadByte", "")]
        public OperateResult<byte> ReadByte(string address)
        {
            return ByteTransformHelper.GetResultFromArray(Read(address, 1));
        }

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__31))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
        //{
        //    <ReadBoolAsync>d__31 stateMachine = new <ReadBoolAsync>d__31();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__32))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__32 stateMachine = new <ReadAsync>d__32();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__33))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadAsync(string[] address, ushort[] length)
        //{
        //    <ReadAsync>d__33 stateMachine = new <ReadAsync>d__33();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadByteAsync>d__34))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte>> ReadByteAsync(string address)
        //{
        //    <ReadByteAsync>d__34 stateMachine = new <ReadByteAsync>d__34();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction + " Please refer to use WriteTag instead ");
        }

        public virtual OperateResult WriteTag(string address, ushort typeCode, byte[] value, int length = 1)
        {
            OperateResult<byte[]> operateResult = BuildWriteCommand(address, typeCode, value, length);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return operateResult2;
            }
            OperateResult operateResult3 = AllenBradleyHelper.CheckResponse(operateResult2.Content);
            if (!operateResult3.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult3);
            }
            return AllenBradleyHelper.ExtractActualData(operateResult2.Content, isRead: false);
        }

        //[AsyncStateMachine(typeof(<WriteAsync>d__37))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__37 stateMachine = new <WriteAsync>d__37();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteTagAsync>d__38))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteTagAsync(string address, ushort typeCode, byte[] value, int length = 1)
        //{
        //    <WriteTagAsync>d__38 stateMachine = new <WriteTagAsync>d__38();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.typeCode = typeCode;
        //    stateMachine.value = value;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("ReadInt16Array", "")]
        public override OperateResult<short[]> ReadInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt16(m, 0, length));
        }

        [HslMqttApi("ReadUInt16Array", "")]
        public override OperateResult<ushort[]> ReadUInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt16(m, 0, length));
        }

        [HslMqttApi("ReadInt32Array", "")]
        public override OperateResult<int[]> ReadInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt32(m, 0, length));
        }

        [HslMqttApi("ReadUInt32Array", "")]
        public override OperateResult<uint[]> ReadUInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt32(m, 0, length));
        }

        [HslMqttApi("ReadFloatArray", "")]
        public override OperateResult<float[]> ReadFloat(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransSingle(m, 0, length));
        }

        [HslMqttApi("ReadInt64Array", "")]
        public override OperateResult<long[]> ReadInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt64(m, 0, length));
        }

        [HslMqttApi("ReadUInt64Array", "")]
        public override OperateResult<ulong[]> ReadUInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt64(m, 0, length));
        }

        [HslMqttApi("ReadDoubleArray", "")]
        public override OperateResult<double[]> ReadDouble(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransDouble(m, 0, length));
        }

        public OperateResult<string> ReadString(string address)
        {
            return ReadString(address, 1, Encoding.UTF8);
        }

        [HslMqttApi("ReadString", "")]
        public override OperateResult<string> ReadString(string address, ushort length)
        {
            return ReadString(address, length, Encoding.UTF8);
        }

        public override OperateResult<string> ReadString(string address, ushort length, Encoding encoding)
        {
            OperateResult<byte[]> operateResult = Read(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            if (operateResult.Content.Length >= 2)
            {
                int count = base.ByteTransform.TransUInt16(operateResult.Content, 0);
                return OperateResult.CreateSuccessResult(encoding.GetString(operateResult.Content, 2, count));
            }
            return OperateResult.CreateSuccessResult(encoding.GetString(operateResult.Content));
        }

        //[AsyncStateMachine(typeof(<ReadInt16Async>d__50))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<short[]>> ReadInt16Async(string address, ushort length)
        //{
        //    <ReadInt16Async>d__50 stateMachine = new <ReadInt16Async>d__50();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<short[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt16Async>d__51))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<ushort[]>> ReadUInt16Async(string address, ushort length)
        //{
        //    <ReadUInt16Async>d__51 stateMachine = new <ReadUInt16Async>d__51();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ushort[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt32Async>d__52))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<int[]>> ReadInt32Async(string address, ushort length)
        //{
        //    <ReadInt32Async>d__52 stateMachine = new <ReadInt32Async>d__52();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt32Async>d__53))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<uint[]>> ReadUInt32Async(string address, ushort length)
        //{
        //    <ReadUInt32Async>d__53 stateMachine = new <ReadUInt32Async>d__53();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFloatAsync>d__54))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<float[]>> ReadFloatAsync(string address, ushort length)
        //{
        //    <ReadFloatAsync>d__54 stateMachine = new <ReadFloatAsync>d__54();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt64Async>d__55))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<long[]>> ReadInt64Async(string address, ushort length)
        //{
        //    <ReadInt64Async>d__55 stateMachine = new <ReadInt64Async>d__55();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt64Async>d__56))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<ulong[]>> ReadUInt64Async(string address, ushort length)
        //{
        //    <ReadUInt64Async>d__56 stateMachine = new <ReadUInt64Async>d__56();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDoubleAsync>d__57))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<double[]>> ReadDoubleAsync(string address, ushort length)
        //{
        //    <ReadDoubleAsync>d__57 stateMachine = new <ReadDoubleAsync>d__57();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__58))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadStringAsync(string address)
        //{
        //    <ReadStringAsync>d__58 stateMachine = new <ReadStringAsync>d__58();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__59))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<string>> ReadStringAsync(string address, ushort length)
        //{
        //    <ReadStringAsync>d__59 stateMachine = new <ReadStringAsync>d__59();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__60))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<string>> ReadStringAsync(string address, ushort length, Encoding encoding)
        //{
        //    <ReadStringAsync>d__60 stateMachine = new <ReadStringAsync>d__60();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.encoding = encoding;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("WriteInt16Array", "")]
        public override OperateResult Write(string address, short[] values)
        {
            return WriteTag(address, 195, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt16Array", "")]
        public override OperateResult Write(string address, ushort[] values)
        {
            return WriteTag(address, 199, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteInt32Array", "")]
        public override OperateResult Write(string address, int[] values)
        {
            return WriteTag(address, 196, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt32Array", "")]
        public override OperateResult Write(string address, uint[] values)
        {
            return WriteTag(address, 200, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteFloatArray", "")]
        public override OperateResult Write(string address, float[] values)
        {
            return WriteTag(address, 202, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteInt64Array", "")]
        public override OperateResult Write(string address, long[] values)
        {
            return WriteTag(address, 197, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt64Array", "")]
        public override OperateResult Write(string address, ulong[] values)
        {
            return WriteTag(address, 201, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteDoubleArray", "")]
        public override OperateResult Write(string address, double[] values)
        {
            return WriteTag(address, 203, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteString", "")]
        public override OperateResult Write(string address, string value)
        {
            byte[] array = string.IsNullOrEmpty(value) ? new byte[0] : Encoding.UTF8.GetBytes(value);
            return WriteTag(address, 208, SoftBasic.SpliceArray<byte>(BitConverter.GetBytes((ushort)array.Length), array));
        }

        [HslMqttApi("WriteBool", "")]
        public override OperateResult Write(string address, bool value)
        {
            return WriteTag(address, 193, (!value) ? new byte[2] : new byte[2]
		{
			255,
			255
		});
        }

        [HslMqttApi("WriteByte", "")]
        public OperateResult Write(string address, byte value)
        {
            return WriteTag(address, 194, new byte[1]
		{
			value
		});
        }

        //[AsyncStateMachine(typeof(<WriteAsync>d__72))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, short[] values)
        //{
        //    <WriteAsync>d__72 stateMachine = new <WriteAsync>d__72();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__73))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ushort[] values)
        //{
        //    <WriteAsync>d__73 stateMachine = new <WriteAsync>d__73();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__74))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, int[] values)
        //{
        //    <WriteAsync>d__74 stateMachine = new <WriteAsync>d__74();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__75))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, uint[] values)
        //{
        //    <WriteAsync>d__75 stateMachine = new <WriteAsync>d__75();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__76))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, float[] values)
        //{
        //    <WriteAsync>d__76 stateMachine = new <WriteAsync>d__76();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__77))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, long[] values)
        //{
        //    <WriteAsync>d__77 stateMachine = new <WriteAsync>d__77();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__78))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ulong[] values)
        //{
        //    <WriteAsync>d__78 stateMachine = new <WriteAsync>d__78();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__79))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, double[] values)
        //{
        //    <WriteAsync>d__79 stateMachine = new <WriteAsync>d__79();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__80))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, string value)
        //{
        //    <WriteAsync>d__80 stateMachine = new <WriteAsync>d__80();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__81))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool value)
        //{
        //    <WriteAsync>d__81 stateMachine = new <WriteAsync>d__81();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__82))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, byte value)
        //{
        //    <WriteAsync>d__82 stateMachine = new <WriteAsync>d__82();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        private byte[] GetLargeForwardOpen()
        {
            return "00 00 00 00 00 00 02 00 00 00 00 00 b2 00 34 00\r\n5b 02 20 06 24 01 06 9c 02 00 00 80 01 00 fe 80\r\n02 00 1b 05 30 a7 2b 03 02 00 00 00 80 84 1e 00\r\ncc 07 00 42 80 84 1e 00 cc 07 00 42 a3 03 20 02\r\n24 01 2c 01".ToHexBytes();
        }

        private byte[] GetAttributeAll()
        {
            return "00 00 00 00 00 00 02 00 00 00 00 00 b2 00 06 00 01 02 20 01 24 01".ToHexBytes();
        }

        public static OperateResult<byte[], ushort, bool> ExtractActualData(byte[] response, bool isRead)
        {
            List<byte> list = new List<byte>();
            int num = 42;
            bool value = false;
            ushort value2 = 0;
            ushort num2 = BitConverter.ToUInt16(response, num);
            if (BitConverter.ToInt32(response, 46) == 138)
            {
                num = 50;
                int num3 = BitConverter.ToUInt16(response, num);
                for (int i = 0; i < num3; i++)
                {
                    int num4 = BitConverter.ToUInt16(response, num + 2 + i * 2) + num;
                    int num5 = (i == num3 - 1) ? response.Length : (BitConverter.ToUInt16(response, num + 4 + i * 2) + num);
                    ushort num6 = BitConverter.ToUInt16(response, num4 + 2);
                    switch (num6)
                    {
                        case 4:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley04
                            };
                        case 5:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley05
                            };
                        case 6:
                            if (response[num + 2] == 210 || response[num + 2] == 204)
                            {
                                return new OperateResult<byte[], ushort, bool>
                                {
                                    ErrorCode = num6,
                                    Message = StringResources.Language.AllenBradley06
                                };
                            }
                            break;
                        case 10:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley0A
                            };
                        case 19:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley13
                            };
                        case 28:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley1C
                            };
                        case 30:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley1E
                            };
                        case 38:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.AllenBradley26
                            };
                        default:
                            return new OperateResult<byte[], ushort, bool>
                            {
                                ErrorCode = num6,
                                Message = StringResources.Language.UnknownError
                            };
                        case 0:
                            break;
                    }
                    if (isRead)
                    {
                        for (int j = num4 + 6; j < num5; j++)
                        {
                            list.Add(response[j]);
                        }
                    }
                }
            }
            else
            {
                byte b = response[num + 6];
                switch (b)
                {
                    case 4:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley04
                        };
                    case 5:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley05
                        };
                    case 6:
                        value = true;
                        break;
                    case 10:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley0A
                        };
                    case 19:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley13
                        };
                    case 28:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley1C
                        };
                    case 30:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley1E
                        };
                    case 38:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.AllenBradley26
                        };
                    default:
                        return new OperateResult<byte[], ushort, bool>
                        {
                            ErrorCode = b,
                            Message = StringResources.Language.UnknownError
                        };
                    case 0:
                        break;
                }
                if (response[num + 4] == 205 || response[num + 4] == 211)
                {
                    return OperateResult.CreateSuccessResult(list.ToArray(), value2, value);
                }
                if (response[num + 4] == 204 || response[num + 4] == 210)
                {
                    for (int k = num + 10; k < num + 2 + num2; k++)
                    {
                        list.Add(response[k]);
                    }
                    value2 = BitConverter.ToUInt16(response, num + 8);
                }
                else if (response[num + 4] == 213)
                {
                    for (int l = num + 8; l < num + 2 + num2; l++)
                    {
                        list.Add(response[l]);
                    }
                }
            }
            return OperateResult.CreateSuccessResult(list.ToArray(), value2, value);
        }
    }
}
